package com.ycb.bookserver.controller.admin;

import com.book.properties.JWTProperties;
import com.book.result.PageResult;
import com.book.result.Result;
import com.ycb.book.dto.BookDTO;
import com.ycb.book.dto.BookPageQueryDTO;
import com.ycb.book.dto.CategoryDTO;
import com.ycb.book.vo.BookVO;
import com.ycb.book.vo.BookVO1;
import com.ycb.bookserver.service.BookService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@Slf4j
@RequestMapping("/admin/book")
public class BookAdminController {
    @Autowired
    private BookService bookService;


    @GetMapping("/booklist")
    @ApiOperation("查询所有书籍")
    public Result searchAllBook(){
        log.info("查询所有书籍");
        List<BookVO> bookVOS=bookService.allBook();
        return Result.success(bookVOS);
    }

    @GetMapping("/booklistcategory")
    @ApiOperation("根据分类查询书籍")
    public Result searchByCategory(CategoryDTO categoryDTO){
        log.info("根据分类查询书籍");
        List<BookVO> bookVOS=bookService.BookCategory(categoryDTO);
        return Result.success(bookVOS);
    }



    @GetMapping("/{id}")
    @ApiOperation("根据id查询书籍信息")
    public Result<BookVO1> getById(@PathVariable Long id){
        log.info("查询书籍信息:{}",id);
        BookVO1 book=bookService.getById(id);
        return Result.success(book);
    }


    @PostMapping("/status/{status}")
    @ApiOperation("修改书籍交易状态信息")
    public Result sold(@PathVariable Integer status,Long id){
        log.info("商品出售:{},{}",status,id);
        bookService.sold(status,id);
        return Result.success();
    }

    @DeleteMapping("/deleteBook")
    @ApiOperation("删除商品")
    public Result delete(Long id){
        log.info("删除商品:{}",id);
        bookService.delete(id);
        return Result.success();
    }

    @DeleteMapping("/deleteBookS")
    @ApiOperation("批量删除书籍")
    public Result deleteBatch(@RequestParam  List<Long> ids){
        log.info("批量删除书籍:{}",ids);
        bookService.deleteBatch(ids);
        return Result.success();
    }

    @GetMapping("/searchByName")
    @ApiOperation("搜索书籍")
    public Result<PageResult> search(BookPageQueryDTO bookPageQueryDTO){
        log.info("书籍分页查询:{}",bookPageQueryDTO);
        PageResult pageResult=bookService.search(bookPageQueryDTO);
        return Result.success(pageResult);
    }
}
